<?php

namespace app\index\controller\consulting;

use app\index\model\UM as UM;
use app\index\service\MessageService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Message 控制器
 * Class Message
 * @package app\consulting\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Message
{
    // 请求参数
    private Request $request;

    // service
    private MessageService $service;

    public function __construct(Request $request, MessageService $messageService)
    {
        $this->request = $request;
        $this->service = $messageService;
    }

    /**
     * 咨询消息列表
     * @param UM $user
     * @return ApiResponse
     * @url /consulting/message/index
     */
    public function index(UM $user): ApiResponse
    {
        $request = $this->request;

        $consultingId = $request->param('consultingId', null);
        $firstId = $request->param('firstId',null);
        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $consultingId,$firstId, $page, $page_size));
    }

    /**
     * 咨询消息详情
     * @param UM $user
     * @return ApiResponse
     */
    public function read(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id', null);

        if(!is_id($id)){
            throw new ValidateException('请选择一条消息');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 发 文本，图片等 消息
     * \app\index\model\UM $user
     * @return ApiResponse
     * @url /consulting/message/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $this->service->add($user, $request->param());

        return ApiResponse::success(null);
    }

}
